// pages/minePage/minePage.js
import Http from '../../utils/http.js';
const http = new Http();
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        //管理员权限
        authority: '07',
        authorityName: '工程师',
        //tabbar背景
        tabbarlist: [
            {
                text: '地图',
                num: 0,
            },
            {
                text: '通答',
                num: 0,
            },
            {
                text: '经验',
                num: 0,
            },
            {
                text: '我的',
                num: 0,
            },
        ],
        tabbarActive: 3,
        ipxBottom: false,
        scrollMove: false,
        //气泡高度
        backHeight: '',
        backTop: '',
        showNavName: false,
        //个人信息
        realname: '',
        realpicurl: '',
        phone: '',
        cityTeam: '湛腾',
        // 打卡信息
        checkincitynum: 0,
        checkincountrynum: 0,
        checkinnum: 0,
        concernNum: 0,
        concernImg: '', //关注列表新消息头像
        enterNum: 0,
        experienceNum: 0,
        verifynum: Number, //审核数
        // 切换nav
        current: 0,
        //日记本
        createuid: '',
        noInfoDiary: false,
        // 日记内容
        experienceArr: [],
        pageNumDiary: 0,
        loadingDiary: true,
        loadingAllDiary: false,
        //日记操作弹框
        show: false,
        actions: [
            {
                name: '删除',
                color: '#FF455E',
            },
        ],
        idDiary: '',
        //照片墙
        // 照片墙--缺省显示
        noInfo: false,
        // 照片墙--上传照片
        sceneImageArr: [],
        // 照片墙--图片描述
        description: [],
        // 照片墙--发布人
        userArr: [],
        userIdArr: [],
        addressArr: [],
        // 照片墙--发布时间
        timeArr: [],
        idArr: [],
        pageNum: 0,
        loading: true,
        loadingAll: false,
        imgLength: 0, //照片墙数量
        // 当前描述索引
        currentPicIndex: 0,
        //云函数
        contentValue: {
            thing2: {
                value: '版本：2.1，4',
            },
            thing3: {
                value: '编辑器',
            },
        },
        // 消息num
        diaryCount: 0, //日记未读数
        enterCount: 0, //录入审核未读数
        exVerifyCount: 0, //经验审核未读数
        checkVerifyCount: 0, //打卡审核未读数
        checkinverifynum: 0, //打卡已审核消息
        enterverifynum: 0, //录入已审核消息
        experienceverifynum: 0, //经验已审核消息
    },
    //我的录入页面
    submitTap(e) {
        // wx.showToast({
        //     title: '敬请期待',
        //     icon: 'none',
        //     duration: 2000,
        // });
        // return;
        wx.navigateTo({
            url: `../../functionPage/pages/minePage/mySubmitInfo/mySubmitInfo?enterNum=${this.data.enterNum}`,
        });
    },
    experienceTap(e) {
        wx.navigateTo({
            url: `../../functionPage/pages/minePage/myExperience/myExperience`,
        });
    },
    // 打卡数页面
    checkTap(e) {
        wx.navigateTo({
            url: `./checkNumber/checkNumber?checkinnum=${this.data.checkinnum}`,
        });
    },
    countryTap(e) {
        wx.navigateTo({
            url: `./countryNumber/countryNumber?checkincountrynum=${this.data.checkincountrynum}`,
        });
    },
    cityTap(e) {
        wx.navigateTo({
            url: `./cityNumber/cityNumber?checkincitynum=${this.data.checkincitynum}`,
        });
    },
    // 消息页面
    newsPage(e) {
        wx.navigateTo({
            url: `../../functionPage/pages/news/minePageNews/minePageNews`,
        });
    },
    // 关注列表
    diaryPage(e) {
        wx.navigateTo({
            url: `../../functionPage/pages/diaryNews/diaryNews`,
        });
    },
    //写日志
    editTap() {
        if (app.globalData.loginPhone == '000000') {
            wx.cloud.callFunction({
                name: 'sendUserNews',
                data: {
                    content: this.data.contentValue,
                    // openidValue: 'oR2cC5daON0YYrOP6e6zueKj31J4' //齐
                    openidValue: 'oR2cC5ZJTCP4MGZmSxIiBzHwnQ7Y', //胡
                },
                success(res) {
                    console.log(res);
                },
                fail(res) {
                    console.log(res);
                },
            });
        } else {
            wx.navigateTo({
                url: `../../releasePage/pages/addDailyRecord/addDailyRecord`,
            });
        }
    },
    // 审核页面
    reviewTap(e) {
        wx.navigateTo({
            url: `../../functionPage/pages/review/reviewList/reviewList`,
        });
    },
    // 口令页面
    jumpWordTap(e) {
        wx.navigateTo({
            url: `../../functionPage/pages/userPassword/userPassword`,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let identity;
        if (app.globalData.authority == '00') {
            identity = '超级管理员';
        } else if (app.globalData.authority == '01') {
            identity = '管理员、技委会成员';
        } else if (
            app.globalData.authority == '04' ||
            app.globalData.authority == '05'
        ) {
            identity = '技委会成员';
        } else if (app.globalData.authority == '06') {
            identity = '工程师';
        } else if (app.globalData.authority == '09') {
            identity = '口令用户';
        } else {
            identity = '外部人员';
        }
        this.setData({
            cityTeam: app.globalData.localcityTeam,
            authority: app.globalData.authority,
            authorityName: identity,
        });
        if (app.globalData.isIpx) {
            this.setData({
                ipxBottom: true,
            });
        }
        this.getInfoList();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        //tabbar
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                selected: 3,
            });
            this.getTabBar().refresh();
        }
        // 获取导航栏气泡高度
        let bubbleInfo = wx.getMenuButtonBoundingClientRect();
        this.setData({
            backHeight: bubbleInfo.height,
            backTop: bubbleInfo.top,
        });
        if (app.globalData.authId) {
            this.setData({
                phone: wx.getStorageSync('loginPhone'),
                realname: app.globalData.realname,
                realpicurl: app.globalData.realpicurl,
            });
        }
        this.getminePageNum();
        this.getDiaryList();
        this.getNewsNum();
        // this.setData({
        //     pageNum: 0,
        //     sceneImageArr: [],
        //     description: [],
        //     userArr: [],
        //     timeArr: [],
        //     idArr: [],
        //     userIdArr: [],
        //     addressArr: [],
        //     loadingAll: false,
        // });
        this.judgeInfoList();
    },
    refresh(e) {
        // this.setData({
        //     pageNum: 0,
        //     sceneImageArr: [],
        //     description: [],
        //     userArr: [],
        //     timeArr: [],
        //     idArr: [],
        //     userIdArr: [],
        //     addressArr: [],
        //     loadingAll: false,
        // })
        // this.getInfoList()
    },
    //写完日记加载
    editBackload() {
        this.setData({
            experienceArr: [],
            pageNumDiary: 0,
            loadingDiary: true,
            loadingAllDiary: false,
        });
        this.getDiaryList();
    },
    //切换nav
    changeNav(e) {
        this.setData({
            current: e.currentTarget.dataset.index,
        });
    },
    swiperChange(e) {
        this.setData({
            current: e.detail.current,
        });
    },
    // 日记本操作
    //显示弹出层
    operate(e) {
        if (e.detail.createuid == app.globalData.uid) {
            this.setData({
                show: true,
                idDiary: e.detail,
            });
            if (typeof this.getTabBar === 'function' && this.getTabBar()) {
                this.getTabBar().setData({
                    tabbarShow: false,
                });
            }
        }
    },
    //取消
    onCancel(e) {
        this.setData(
            {
                show: false,
            },
            () => {
                if (typeof this.getTabBar === 'function' && this.getTabBar()) {
                    this.getTabBar().setData({
                        tabbarShow: true,
                    });
                }
            }
        );
    },
    onDelect(e) {
        let _that = this;
        wx.request({
            url: app.globalData.rootPath + `cjMap/deleteDiary`,
            method: 'GET',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
            },
            data: {
                authId: app.globalData.authId,
                id: e.currentTarget.dataset.id,
            },
            success: function (res) {
                _that.setData(
                    {
                        show: false,
                        experienceArr: [],
                        pageNumDiary: 0,
                        loadingDiary: true,
                        loadingAllDiary: false,
                    },
                    () => {
                        if (
                            typeof _that.getTabBar === 'function' &&
                            _that.getTabBar()
                        ) {
                            _that.getTabBar().setData({
                                tabbarShow: true,
                            });
                        }
                    }
                );
                _that.editBackload();
            },
        });
    },
    // 图片预览
    imgPreviewTap(e) {
        this.setData({
            currentPicIndex: e.currentTarget.dataset.current,
        });
        wx.setStorageSync('minePageImgLength', this.data.imgLength);
        wx.navigateTo({
            url: `../checkIn/imgPreview/imgPreview?fromWhere=00`,
        });
    },
    //顶部显示姓名
    scrolltoupper(e) {
        // console.log(e.detail.scrollTop)
        if (e.detail.scrollTop > 108 || e.detail.scrollTop == undefined) {
            this.setData({
                showNavName: true,
            });
        } else {
            this.setData({
                showNavName: false,
            });
        }
        if (e.detail.scrollTop > 340) {
            this.setData({
                scrollMove: true,
            });
        } else {
            this.setData({
                scrollMove: false,
            });
        }
    },
    scrolltoupperBtm(e) {
        // this.setData({
        //     scrollMove: false
        // })
    },
    //获取个人信息
    getminePageNum(e) {
        let _that = this;
        wx.request({
            url: app.globalData.rootPath + `cjMap/personalPageNum`,
            method: 'GET',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
            },
            data: {
                authId: app.globalData.authId,
            },
            success: function (res) {
                console.log(res.data);
                _that.setData({
                    checkincitynum: res.data.data.checkincitynum,
                    checkincountrynum: res.data.data.checkincountrynum,
                    checkinnum: res.data.data.checkinnum,
                    concernNum: res.data.data.concernnum,
                    concernImg: res.data.data.latestUserInfo
                        ? res.data.data.latestUserInfo.picurl
                        : '',
                    enterNum: res.data.data.enternum,
                    experienceNum: res.data.data.experiencenum,
                    verifynum: res.data.data.verifynum,
                    checkinverifynum: res.data.data.checkinverifynum,
                    enterverifynum: res.data.data.enterverifynum,
                    experienceverifynum: res.data.data.experienceverifynum,
                });
            },
        });
    },
    //获取日记列表
    getDiaryList(e) {
        let _that = this;
        if (_that.data.pageNumDiary == 0 || _that.data.current == 1) {
            if (!_that.data.loadingAllDiary) {
                _that.setData({
                    pageNumDiary: _that.data.pageNumDiary + 1,
                    loadingDiary: true,
                    loadingAllDiary: false,
                });
                wx.request({
                    url: app.globalData.rootPath + `cjMap/getDiaryList`,
                    method: 'GET',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded',
                    },
                    data: {
                        authId: app.globalData.authId,
                        createuid: _that.data.createuid, //如果不填就是返本人的
                        pageNo: _that.data.pageNumDiary,
                        pageSize: 10,
                    },
                    success: function (res) {
                        console.log(res);
                        if (res.data.status == 0) {
                            for (let i = 0; i < res.data.data.length; i++) {
                                let descValue = {
                                    id: '',
                                    time: '2019-12-12 13:28',
                                    describeText: '',
                                    createuid: '',
                                };
                                let arrValue = {
                                    describe: {},
                                    imageArr: [],
                                };
                                descValue.id = res.data.data[i].id;
                                descValue.time =
                                    res.data.data[i].createtime.slice(0, 4) +
                                    '-' +
                                    res.data.data[i].createtime.slice(5, 7) +
                                    '-' +
                                    res.data.data[i].createtime.slice(8, 10) +
                                    ' ' +
                                    res.data.data[i].createtime.slice(11, 16);
                                descValue.describeText =
                                    res.data.data[i].content;
                                descValue.createuid =
                                    res.data.data[i].createuid;
                                arrValue.describe = descValue;
                                arrValue.imageArr = res.data.data[i].picurls;
                                _that.data.experienceArr.push(arrValue);
                            }
                            _that.setData({
                                experienceArr: _that.data.experienceArr,
                                loadingDiary: false,
                            });
                            if (res.data.data.length < 10) {
                                _that.setData({
                                    loadingAllDiary: true,
                                });
                            }
                            if (!_that.data.experienceArr.length) {
                                _that.setData({
                                    noInfoDiary: true,
                                    loadingDiary: false,
                                    loadingAllDiary: false,
                                });
                            } else {
                                _that.setData({
                                    noInfoDiary: false,
                                });
                            }
                        } else {
                            if (res.data.errmsg == '输入页码超出范围') {
                                _that.setData({
                                    loadingAllDiary: true,
                                    loadingDiary: false,
                                });
                            }
                        }
                    },
                });
            }
        }
    },
    // 判断是否加载照片墙、
    judgeInfoList() {
        let _that = this;
        let data = {
            authId: app.globalData.authId,
            personalflg: '00', //查个人页照片墙填 00
            pageSize: 10,
            pageNo: 1,
        };
        http.getRequest('cjMap/getPhotoWallList', data).then((res) => {
            if (res.status == 0) {
                console.log(wx.getStorageSync('minePageImgLength'));
                if (
                    res.pager.totalRows !=
                    wx.getStorageSync('minePageImgLength')
                ) {
                    _that.setData(
                        {
                            pageNum: 0,
                            sceneImageArr: [],
                            description: [],
                            userArr: [],
                            timeArr: [],
                            idArr: [],
                            userIdArr: [],
                            addressArr: [],
                            loadingAll: false,
                        },
                        () => {
                            _that.getInfoList();
                        }
                    );
                }
            }
        });
    },
    //获取照片墙
    getInfoList(e) {
        let _that = this;
        if (_that.data.pageNum == 0 || _that.data.current == 0) {
            if (!_that.data.loadingAll) {
                _that.setData({
                    pageNum: _that.data.pageNum + 1,
                    loading: true,
                    loadingAll: false,
                });
                wx.request({
                    url: app.globalData.rootPath + `cjMap/getPhotoWallList`,
                    method: 'GET',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded',
                    },
                    data: {
                        authId: app.globalData.authId,
                        personalflg: '00', //查个人页照片墙填 00
                        pageSize: 10,
                        pageNo: _that.data.pageNum,
                    },
                    success: function (res) {
                        console.log(res);
                        if (res.data.status == 0) {
                            //重置缓存
                            wx.setStorageSync(
                                'minePageImgLength',
                                res.data.pager.totalRows
                            );
                            for (let i = 0; i < res.data.data.length; i++) {
                                _that.data.sceneImageArr.push(
                                    res.data.data[i].picurl
                                );
                                _that.data.description.push(
                                    res.data.data[i].picdescription
                                );
                                _that.data.userArr.push(
                                    res.data.data[i].createuname
                                );
                                _that.data.addressArr.push(
                                    res.data.data[i].cityname
                                );
                                let checkinTime = app.timeago(
                                    res.data.data[i].checkintime ||
                                        res.data.data[i].createtime
                                );
                                _that.data.timeArr.push(checkinTime);
                                _that.data.idArr.push(res.data.data[i].id);
                                _that.data.userIdArr.push(
                                    res.data.data[i].createuid
                                );
                            }
                            _that.setData({
                                sceneImageArr: _that.data.sceneImageArr,
                                description: _that.data.description,
                                userArr: _that.data.userArr,
                                timeArr: _that.data.timeArr,
                                idArr: _that.data.idArr,
                                addressArr: _that.data.addressArr,
                                userIdArr: _that.data.userIdArr,
                                loading: false,
                                imgLength: res.data.pager.totalRows,
                            });
                            if (res.data.data.length < 10) {
                                _that.setData({
                                    loadingAll: true,
                                });
                            }
                            if (!_that.data.sceneImageArr.length) {
                                _that.setData({
                                    noInfo: true,
                                    loading: false,
                                    loadingAll: false,
                                });
                            } else {
                                _that.setData({
                                    noInfo: false,
                                });
                            }
                        } else {
                            if (res.data.errmsg == '输入页码超出范围') {
                                _that.setData({
                                    loadingAll: true,
                                    loading: false,
                                });
                            }
                        }
                    },
                });
            }
        }
    },
    // 获取消息数量
    getNewsNum() {
        let _that = this;
        wx.request({
            url: app.globalData.rootPath + 'cjMap/getUnreadMsgCnt',
            header: {
                'content-type': 'application/json',
            },
            data: {
                authId: app.globalData.authId,
            },
            success(res) {
                console.log(res.data.data);
                app.globalData.nqModuleCount = res.data.data.nqModuleCount;
                app.globalData.exModuleCount = res.data.data.exModuleCount;
                app.globalData.mineModuleCount = res.data.data.mineModuleCount;
                let arr1 = 'tabbarlist[1].num';
                let arr2 = 'tabbarlist[2].num';
                let arr3 = 'tabbarlist[3].num';
                _that.setData({
                    [arr1]: res.data.data.nqModuleCount,
                    [arr2]: res.data.data.exModuleCount,
                    [arr3]: res.data.data.mineModuleCount,
                    diaryCount: res.data.data.diaryCount,
                    enterCount: res.data.data.enterCount,
                    exVerifyCount: res.data.data.exVerifyCount,
                    checkVerifyCount: res.data.data.checkVerifyCount,
                });
                if (
                    typeof _that.getTabBar === 'function' &&
                    _that.getTabBar()
                ) {
                    _that.getTabBar().setData({
                        selected: 3,
                        tabbarShow: true,
                    });
                    _that.getTabBar().refresh();
                }
            },
        });
    },
    onTabbarChange(event) {
        this.setData({ active: event.detail });
        let url;
        if (event.detail == 0) {
            url = '/pages/index/index';
        }
        if (event.detail == 1) {
            url = '/pages/notice/notice';
        }
        if (event.detail == 2) {
            url = '/pages/experience/experience';
        }
        if (event.detail == 3) {
            url = '/pages/minePage/minePage';
        }
        wx.switchTab({
            url: url,
        });
    },
    // 点击标题回到顶部
    doubleTap() {
        if (this.data.current == 0) {
            this.setData({
                toView: 'photo',
                toViewPage: 'mine-area',
            });
        } else {
            this.setData({
                toView: 'diary',
                toViewPage: 'mine-area',
            });
        }
    },
});
